import akshare as ak
import numpy as np
from sqlalchemy import create_engine

# 股票代码
stock_code = '000001'

# 获取数据
stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=stock_code, period="daily",
                                        adjust="")

# 给新数组填充数据
a = np.array([],dtype=object)
pad_arr = np.pad(a, (0, len(stock_zh_a_hist_df)), 'constant', constant_values=stock_code)

# 新增一列
stock_zh_a_hist_df.insert(0, "股票代码", pad_arr.tolist(), True)

# 更改表头字段
stock_zh_a_hist_df.columns = ['stock_code', 'date_str', 'kaipan_price', 'shoupan_price', 'max_price', 'min_price',
                              'chengjiao_quantity', 'chengjiao_amount'
    , 'zhenfu', 'zhang_die_fu', 'zhang_die_e', 'huanshoulv']
print(stock_zh_a_hist_df)


# 将 DataFrame 保存到数据库
engine = create_engine('mysql+pymysql://root:root@localhost:3306/test')
stock_zh_a_hist_df.to_sql(name='stock_history', con=engine, if_exists='replace', index=False)